package com.woniuxy.enjoylife.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.enjoylife.entity.Recent;
import com.woniuxy.enjoylife.entity.RecentAgree;
import com.woniuxy.enjoylife.entity.RecentComment;
import com.woniuxy.enjoylife.entity.dto.*;
import com.woniuxy.enjoylife.mapper.RecentAgreeMapper;
import com.woniuxy.enjoylife.mapper.RecentCommentMapper;
import com.woniuxy.enjoylife.mapper.RecentMapper;
import com.woniuxy.enjoylife.mapper.UserMapper;
import com.woniuxy.enjoylife.service.RecentCommentService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.RowId;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态的评论
 */
@Service
public class RecentCommentServiceImpl implements RecentCommentService {

    @Resource
    private RecentMapper recentMapper;
    @Resource
    private RecentAgreeMapper recentAgreeMapper;
    @Resource
    private RecentCommentMapper recentCommentMapper;
    @Resource
    private UserMapper userMapper;


    //查询所有评论
    @Override
    public PageResult<RecentDto> selectComment(int page, int size, int uid) {

        return null;
    }

    //查询单个评论
    @Override
    public List<RecentCommentDto2> selectSingleComment(int rid) {
//        RecentCommentDto2 recentCommentDto2 = new RecentCommentDto2();
//        recentCommentDto2.getRid();
//        ArrayList<RecentCommentDto2> list = new ArrayList();
//        list.add(recentCommentDto2);
//        String name = userMapper.selectById(recentCommentDto2.getUid()).getAccount();
        return recentCommentMapper.selectSingleComment(rid);
    }

    //新增评论
    @Override
    public int addComment(RecentComment recentComment,int userid) {
        recentComment.setUid(userid);
        return recentCommentMapper.addComment(recentComment);
    }

    //修改评论
    @Override
    public int updateComment(RecentComment recentComment) {
        return recentCommentMapper.updateComment(recentComment);
    }

    //删除评论
    @Override
    public int deleteComment(int id) {
        return recentCommentMapper.deleteComment(id);
    }

    //************************平台角度***************
    /**
     * 查询所有评论
     * @return
     */
    @Override
    public PageResult<List<RecentDetailDto>> findAll(String content,int current, int size) {
        if(content!=null){
            //模糊查询
            Page<Object> objects = PageHelper.startPage(current, size);
            List<RecentDetailDto> list = recentCommentMapper.findAllByName(content);
            PageResult pageResult = new PageResult().setData(list).setTotal(objects.getTotal()).setTotalPage(objects.getPageNum());
            return pageResult;
        }else{
            //获取分页对象
            Page<Object> objects = PageHelper.startPage(current, size);
            List<RecentDetailDto> list = recentCommentMapper.findAll();
            PageInfo<RecentDetailDto> pageInfo = new PageInfo<>(list);
            PageResult pageResult = new PageResult().setData(list).setTotal(objects.getTotal()).setTotalPage(objects.getPageNum());

            return pageResult;
        }
    }
    /**
     * 恢复某条已删除评论
     * @param id
     * @return
     */
    @Override
    public boolean restorecomment(int id) {
        return recentCommentMapper.restorecomment(id)>0?true:false;
    }

    /**
     * 模糊查询对应的评论
     * @param content
     * @return
     */
    @Override
    public PageResult<List<RecentDetailDto>> findByLike(String content,int current,int size) {
        //获取分页对象
        PageHelper.startPage(current,size);
        List<RecentDetailDto> list = recentCommentMapper.findByLike(content);
        PageInfo<RecentDetailDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    /**
     * 查询动态的评论和评论人信息
     * @param id
     * @return
     */
    @Override
    public List<RecentCommentDto> findRecentDetail(int id) {
        return recentCommentMapper.findRecentDetail(id);
    }

    /**
     * 查询点赞数量
     * @param rid
     * @return
     */
    @Override
    public int findAggreeNum(int rid) {

            return recentAgreeMapper.findAggreeNum(rid);

    }

    /**
     * 查询评论数量
     * @param rid
     * @return
     */
    @Override
    public int findCommentNum(int rid) {
        return recentCommentMapper.findCommentNum(rid);
    }

}
